﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;


namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class AddItem : Form,IItemView
    {
        public event EventHandler OnAddComplete;

        private string questionId;

        public IItemController controller;

        public AddItem(string questionId)
        {
            InitializeComponent();
            controller = new ItemController(this);
            this.questionId = questionId;
        }

        void Clear()
        {
            txtContent.Text = "";
            txtTitle.Text = "";
            cbDefault.Checked = false;
            txtImage.Text = string.Empty;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddItem_FormClosing(object sender, FormClosingEventArgs e)
        {
            OnAddComplete(null, null);
        }

        public void CreateItemComplete(string message)
        {
            MessageBox.Show(message);
            Clear();
        }

        public void EditItemComplete(string message)
        {
            throw new NotImplementedException();
        }

        public void GetItemComplete(VT_Items item)
        {
            throw new NotImplementedException();
        }

        private void btnImage_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "JPEG文件(*.jpg)|*.jpg|PNG文件(*.png)|*.png|GIF文件(*.gif)|*.gif";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string filePath = openFileDialog1.FileName;
                txtImage.Text = filePath;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (cbProject.Checked && cbbProject.SelectedItem != null)
            //{
            //    VT_Projects project = cbbProject.SelectedItem as VT_Projects;
            //    controller.CreateItem(
            //      questionId,
            //      project.Project_ID,
            //      txtTitle.Text,
            //      txtContent.Text,
            //      project.ProjectImage.Project_Image,
            //      (cbDefault.Checked ? 1 : 0)
            //      );

            //}
            //else
            //{
                controller.CreateItem(
                   questionId,
                   null,
                   txtTitle.Text,
                   txtContent.Text,
                   txtImage.Text,
                   (cbDefault.Checked ? 1 : 0)
                   );
            //}
        }

        private void cbProject_CheckedChanged(object sender, EventArgs e)
        {
            cbbProject.Visible = cbProject.Checked;

            txtContent.Enabled = !cbProject.Checked;
            txtTitle.Enabled = !cbProject.Checked;
            txtImage.Enabled = !cbProject.Checked;
            btnImage.Enabled = !cbProject.Checked;
        }

        public void GetProjectsComplete(IList<VT_Projects> projects)
        {
            cbbProject.DataSource = projects;
        }

        private void AddItem_Load(object sender, EventArgs e)
        {
            //controller.GetProjects();
        }

        private void cbbProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbProject.Checked && cbbProject.SelectedItem != null)
            {
                VT_Projects project = cbbProject.SelectedItem as VT_Projects;
                txtContent.Text = project.Project_Description;
                txtTitle.Text = project.Project_Name;
            }
        }

        public void OnSubmitError(string message)
        {
            MessageBox.Show(message);
        }


        public void CreateItemsComplete(string message)
        {
            throw new NotImplementedException();
        }

        public void GetItemsComplete(IList<VT_Items> item)
        {
            throw new NotImplementedException();
        }
    }
}
